package com.niodata.dp.task.entity;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializable;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.function.BiConsumer;

/**
 * plugin category stat.
 * <p> [ { "sysCategoryCode":"basic", "sysCategoryName":"通用",
 * " funcategories":[ { "func":"a", "count":12 }, { "func":"b", "count":13 } ] }, {
 * "sysCategoryCode":"basic2", "sysCategoryName":"通用2", "funcategories":[ { "func":"a", "count":12
 * }, { "func":"b", "count":13 } ] } ] </p>
 */
public class PluginCategoryCountSta extends JsonSerializable.Base {


  private Map<String, Map<String, Integer>> sysMap;

  public PluginCategoryCountSta(List<PluginCategoryCountItem> list) {
    sysMap = new TreeMap<>();
    for (PluginCategoryCountItem pc : list) {
      Map<String, Integer> map = sysMap.get(pc.getSysCategory());
      if (map == null) {
        map = new HashMap<>();
        sysMap.put(pc.getSysCategory(), map);
      }
      map.put(pc.getFuncCategory(), pc.getCount());
    }
  }


  @Override
  public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException {
    gen.writeStartArray();
    for (String sysCode : sysMap.keySet()) {
      Map<String, Object> sysCatMap = new HashMap();
      sysCatMap.put("sysCategoryCode", sysCode);
      sysCatMap.put("sysCategoryName", PluginSysCategory.valueOf(sysCode).getName());
      List<Map<String, Object>> funcs = new ArrayList<>();
      sysMap.get(sysCode).forEach(new BiConsumer<String, Integer>() {
        @Override
        public void accept(String s, Integer integer) {
          Map<String, Object> map = new HashMap<>(2);
          map.put("func", s);
          map.put("count", integer);
          funcs.add(map);
        }
      });
      sysCatMap.put("funcCategories", funcs);
      gen.writeObject(sysCatMap);
    }
    gen.writeEndArray();
  }

  @Override
  public void serializeWithType(JsonGenerator gen, SerializerProvider serializers,
      TypeSerializer typeSer) throws IOException {
    gen.writeStartArray();
    for (String sysCode : sysMap.keySet()) {
      Map<String, Object> sysCatMap = new HashMap();
      sysCatMap.put("sysCategoryCode", sysCode);
      sysCatMap.put("sysCategoryName", PluginSysCategory.valueOf(sysCode).getName());
      List<Map<String, Object>> funcs = new ArrayList<>();
      sysMap.get(sysCode).forEach(new BiConsumer<String, Integer>() {
        @Override
        public void accept(String s, Integer integer) {
          Map<String, Object> map = new HashMap<>(2);
          map.put("func", s);
          map.put("count", integer);
          funcs.add(map);
        }
      });
      sysCatMap.put("funcCategories", funcs);
      gen.writeObject(sysCatMap);
    }
    gen.writeEndArray();
  }


}
